package com.example.fouroperations;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    public static final String TAG ="MainActivity";
    ArrayList<String> ques_list; //存放生成的题目
    ArrayList<String> ans_list; //存放生成题目的答案

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: HI!");
    }

    //点击“Start”
    public void startClick(View btn) {
        Log.i(TAG, "startClick: ");

        //获取用户输入题目个数
        EditText quesNum = findViewById(R.id.quesNum);
        String ques_temp = quesNum.getText().toString();

        //获取用户输入每题作答时间
        EditText countdownSet = findViewById(R.id.countdownSet);
        String countdown_temp = countdownSet.getText().toString();

        //获取用户输入每题分数
        EditText preScoreSet = findViewById(R.id.preScoreSet);
        String preScore_temp = preScoreSet.getText().toString();

        if(ques_temp.equals("")||countdown_temp.equals("")||preScore_temp.equals("")){
            //有未填写内容——提示
            Toast.makeText(MainActivity.this, R.string.unfilledTip, Toast.LENGTH_SHORT).show();
        }
        else {
            int ques_num = Integer.valueOf(ques_temp);
            int countdown = Integer.valueOf(countdown_temp);
            int preScore = Integer.valueOf(preScore_temp);

            if (ques_num == 0 || countdown == 0 || preScore == 0) {
                //填写0——提示
                Toast.makeText(MainActivity.this, R.string.zeroTip, Toast.LENGTH_SHORT).show();
            } else {

                //生成题目及其答案
                ques_list = new ArrayList<String>();
                ans_list = new ArrayList<String>();
                BinaryTree bTree;

                for (int num = 0; num < ques_num; num++) {
                    bTree = new BinaryTree(2);
                    bTree.createBTree();
                    ques_list.add(bTree.toString() + "");
                    String result = bTree.CalAndVal();
                    ans_list.add(String.format("%.2f",Float.parseFloat(result)));
                }

                Log.i(TAG, "startClick: ques_list=" + ques_list);
                Log.i(TAG, "startClick: ans_list=" + ans_list);
                Log.i(TAG, "startClick: ques_num=" + ques_num);
                Log.i(TAG, "startClick: countdown=" + countdown);

                //使用Buddle打包发送数据
                Intent mainIntent = new Intent(this, com.example.fouroperations.AnswerActivity.class);

                Bundle mainBdl = new Bundle();
                mainBdl.putStringArrayList("ques_list", ques_list);
                mainBdl.putStringArrayList("ans_list", ans_list);
                mainBdl.putInt("countdown", countdown);
                mainBdl.putInt("ques_num", ques_num);
                mainBdl.putInt("preScore", preScore);
                mainIntent.putExtras(mainBdl);

                startActivity(mainIntent);

            }

        }
    }

}